package com.demodis.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.demodis.dao.PermissionDaoI;
import com.demodis.domain.TblPermission;
import com.demodis.util.DBUtil;

public class PermissionDaoImpl extends BaseDao implements PermissionDaoI {

	@Override
	public List<TblPermission> getPermissions() {
		String sql = "SELECT * FROM TBL_PERMISSION;";
		List<TblPermission> list = new ArrayList<TblPermission>();
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				TblPermission perm = new TblPermission();
				perm.setPermId(rs.getString("permId"));
				perm.setPermName(rs.getString("permName"));
				perm.setPermValue(rs.getInt("permValue"));
				perm.setPermUrl(rs.getString("permUrl"));
				perm.setModId(rs.getInt("modId"));
				list.add(perm);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return list;
	}

	@Override
	public boolean save(TblPermission perm) {
		String sql1 = "SELECT MAX(PERMVALUE) FROM TBL_PERMISSION";
		String sql2 = "INSERT INTO TBL_PERMISSION(PERMID, PERMNAME, PERMVALUE, PERMURL, MODID) VALUES(?, ?, ?, ?, ?);";
		conn = DBUtil.getConnection();
		int i = 0;
		try {
			ps = conn.prepareStatement(sql1);
			rs = ps.executeQuery();
			int permValue = 0;
			if (rs.next()) {
				permValue = rs.getInt(1);
			}

			ps = conn.prepareStatement(sql2);
			ps.setString(1, perm.getPermId());
			ps.setString(2, perm.getPermName());
			ps.setInt(3, permValue + 1);
			ps.setString(4, perm.getPermUrl());
			ps.setInt(5, perm.getModId());

			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}

	@Override
	public boolean modify(TblPermission perm) {
		String sql = "UPDATE TBL_PERMISSION SET PERMNAME = ?, PERMURL = ?, MODID = ? WHERE PERMID = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);

			ps.setString(1, perm.getText());
			ps.setString(2, perm.getUrl());
			ps.setInt(3, perm.getPid());
			ps.setString(4, perm.getId());

			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1 ? true : false;
	}

	@Override
	public boolean delete(TblPermission perm) {
		String sql = "DELETE FROM TBL_PERMISSION WHERE PERMID IN(?);";
		int i = 0;
		try {
			conn = DBUtil.getConnection();

			ps = conn.prepareStatement(sql);
			ps.setString(1, perm.getId());
			i = ps.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i > 0;
	}

	@Override
	public boolean distribute(TblPermission perm) {
		String sql = "UPDATE TBL_ROLE SET ROLEPERMVALUE = ? WHERE ROLEID = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);

			ps.setInt(1, perm.getPermValue());
			ps.setInt(2, perm.getRoleId());

			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}

		return i == 1;
	}

	@Override
	public Integer getUserPermValue(Integer userId) {
		String sql = "SELECT ROLEPERMVALUE FROM TBL_ROLE WHERE ROLEID IN(SELECT ROLEID FROM TBL_USER WHERE USERID = ?);";

		int permValue = 0;
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			if (rs.next()) {
				permValue = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return permValue;
	}

	@Override
	public Integer getPermValue(String permUrl) {
		String sql = "SELECT PERMVALUE FROM TBL_PERMISSION WHERE PERMURL = ?;";

		int permValue = 0;
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, permUrl);
			rs = ps.executeQuery();
			if (rs.next()) {
				permValue = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return permValue;
	}

}
